﻿using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.DependencyInjection;
using YunxSoft.Abp.LanguageManagement.Localization;
using YunxSoft.Abp.LanguageManagement.Web.Menus;
using Volo.Abp.AspNetCore.Mvc.Localization;
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared;
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.PageToolbars;
using Volo.Abp.AutoMapper;
using Volo.Abp.Localization;
using Volo.Abp.Modularity;
using Volo.Abp.UI.Navigation;
using Volo.Abp.VirtualFileSystem;
using YunxSoft.Abp.LanguageManagement.Permissions;

namespace YunxSoft.Abp.LanguageManagement.Web;

[DependsOn(
    typeof(LanguageManagementApplicationContractsModule),
    typeof(AbpAspNetCoreMvcUiThemeSharedModule),
    typeof(AbpAutoMapperModule)
    )]
public class LanguageManagementWebModule : AbpModule
{
    public override void PreConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.PreConfigure<AbpMvcDataAnnotationsLocalizationOptions>(options =>
        {
            options.AddAssemblyResource(typeof(LanguageManagementResource), typeof(LanguageManagementWebModule).Assembly);
        });

        PreConfigure<IMvcBuilder>(mvcBuilder =>
        {
            mvcBuilder.AddApplicationPartIfNotExists(typeof(LanguageManagementWebModule).Assembly);
        });
    }

    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        Configure<AbpNavigationOptions>(options =>
        {
            options.MenuContributors.Add(new LanguageManagementMenuContributor());
        });

        Configure<AbpVirtualFileSystemOptions>(options =>
        {
            options.FileSets.AddEmbedded<LanguageManagementWebModule>();
        });

        context.Services.AddAutoMapperObjectMapper<LanguageManagementWebModule>();
        Configure<AbpAutoMapperOptions>(options =>
        {
            options.AddMaps<LanguageManagementWebModule>(validate: true);
        });

        Configure<RazorPagesOptions>(options =>
        {
            options.Conventions.AuthorizePage("/LanguageManagement/Languages/Index", LanguageManagementPermissions.Languages.Default);
            options.Conventions.AuthorizePage("/LanguageManagement/Languages/CreateModal", LanguageManagementPermissions.Languages.Create);
            options.Conventions.AuthorizePage("/LanguageManagement/Languages/EditModal", LanguageManagementPermissions.Languages.Update);
            
            options.Conventions.AuthorizePage("/LanguageManagement/LanguageTexts/Index", LanguageManagementPermissions.LanguageTexts.Default);
            options.Conventions.AuthorizePage("/LanguageManagement/LanguageTexts/EditModal", LanguageManagementPermissions.LanguageTexts.Update);
        });
        
        Configure<AbpPageToolbarOptions>(options =>
        {
            options.Configure<YunxSoft.Abp.LanguageManagement.Web.Pages.LanguageManagement.Languages.IndexModel>(
                toolbar =>
                {
                    toolbar.AddButton(
                        LocalizableString.Create<LanguageManagementResource>("CreateNewLanguage"),
                        icon: "plus",
                        name: "CreateNewLanguage",
                        requiredPolicyName: LanguageManagementPermissions.Languages.Create
                    );
                }
            );
        });
    }
}
